<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/5.13.0/css/all.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"horsenliu.gitee.io","root":"/blog/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","width":240,"display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="1. 简介Redis：REmote DIctionary Server（远程字典服务器） 是完全开源免费的，用 C 语言编写的，遵守 BSD 协议，是一个高性能的（Key&#x2F;Value）分布式内存数据库，基于内存运行，并支持持久化的NoSQL 数据库，是当前最热门的 NoSQL 数据库之一，也被人们称为数据结构服务器  Redis 与其他 key-value 缓存产品有以下三个特点  Re">
<meta property="og:type" content="article">
<meta property="og:title" content="02_Redis 入门">
<meta property="og:url" content="http://horsenliu.gitee.io/blog/posts/4e5f8254.html">
<meta property="og:site_name" content="霍森的笔记本">
<meta property="og:description" content="1. 简介Redis：REmote DIctionary Server（远程字典服务器） 是完全开源免费的，用 C 语言编写的，遵守 BSD 协议，是一个高性能的（Key&#x2F;Value）分布式内存数据库，基于内存运行，并支持持久化的NoSQL 数据库，是当前最热门的 NoSQL 数据库之一，也被人们称为数据结构服务器  Redis 与其他 key-value 缓存产品有以下三个特点  Re">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/4e5f8254/image-20201125082919058.png">
<meta property="og:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/4e5f8254/image-20201125083032915.png">
<meta property="og:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/4e5f8254/image-20201125093215235.png">
<meta property="og:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/4e5f8254/image-20201125101740541.png">
<meta property="article:published_time" content="2020-11-24T10:48:00.000Z">
<meta property="article:modified_time" content="2021-07-28T12:58:39.286Z">
<meta property="article:author" content="Horsen Liu">
<meta property="article:tag" content="环境搭建">
<meta property="article:tag" content="Redis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/4e5f8254/image-20201125082919058.png">

<link rel="canonical" href="http://horsenliu.gitee.io/blog/posts/4e5f8254.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>02_Redis 入门 | 霍森的笔记本</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">霍森的笔记本</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记的不少 会的不多</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">123</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">36</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">220</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/horsenliu" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://horsenliu.gitee.io/blog/posts/4e5f8254.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.png">
      <meta itemprop="name" content="Horsen Liu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="霍森的笔记本">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          02_Redis 入门
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-11-24 18:48:00" itemprop="dateCreated datePublished" datetime="2020-11-24T18:48:00+08:00">2020-11-24</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">常用组件学习笔记</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/Redis/" itemprop="url" rel="index"><span itemprop="name">Redis</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>3.4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>3 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="1-简介"><a href="#1-简介" class="headerlink" title="1. 简介"></a>1. 简介</h3><p>Redis：<strong>RE</strong>mote <strong>DI</strong>ctionary <strong>S</strong>erver（远程字典服务器）</p>
<p>是完全开源免费的，用 C 语言编写的，遵守 BSD 协议，是一个高性能的（Key&#x2F;Value）分布式内存数据库，基于内存运行，并支持持久化的NoSQL 数据库，是当前最热门的 NoSQL 数据库之一，也被人们称为<br>数据结构服务器 </p>
<p>Redis 与其他 key-value 缓存产品有以下三个特点</p>
<ul>
<li>Redis 支持数据的持久化，可以将内存中的数据保持在磁盘中，重启的时候可以再次加载进行使用。</li>
<li>Redis 不仅仅支持简单的 key-value 类型的数据，同时还提供 list、set、zset、hash 等数据结构的存储。</li>
<li>Redis 支持数据的备份，即 master-slave 模式的数据备份。</li>
</ul>
<h3 id="2-功能和特性"><a href="#2-功能和特性" class="headerlink" title="2. 功能和特性"></a>2. 功能和特性</h3><ul>
<li>内存存储和持久化：Redis 支持异步将内存中的数据写到硬盘上，同时不影响继续服务</li>
<li>取最新 N 个数据的操作，例如可以将最新的 10 条评论的 ID 放在 Redis 的 List 集合里面</li>
<li>发布、订阅消息系统</li>
<li>地图信息分析</li>
<li>定时器、计数器</li>
<li>数据类型、基本操作和配置</li>
<li>持久化和复制，RDB、AOF</li>
<li>事务的控制</li>
<li>……</li>
</ul>
<span id="more"></span>

<h3 id="3-安装-Redis"><a href="#3-安装-Redis" class="headerlink" title="3. 安装 Redis"></a>3. 安装 Redis</h3><h4 id="3-1-Windows"><a href="#3-1-Windows" class="headerlink" title="3.1 Windows"></a>3.1 Windows</h4><ol>
<li><p>下载安装包</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/microsoftarchive/redis/releases">https://github.com/microsoftarchive/redis/releases</a></p>
</blockquote>
</li>
<li><p>解压到环境目录</p>
</li>
<li><p><code>redis-cli.exe</code>是客户端，<code>redis-server.exe</code>是服务器</p>
<img src="/blog/blog/posts/blog/posts/4e5f8254/image-20201125082919058.png" class>
</li>
<li><p>打开服务器，可以看到默认的端口号是 6379</p>
</li>
<li><p>打开客户端，输入命令<code>ping</code>，如果出现<code>PONG</code>，说明连接成功了，可以建立一个键值对测试一下</p>
<img src="/blog/blog/posts/blog/posts/4e5f8254/image-20201125083032915.png" class></li>
</ol>
<h4 id="3-2-Linux"><a href="#3-2-Linux" class="headerlink" title="3.2 Linux"></a>3.2 Linux</h4><ol>
<li><p>下载压缩包</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://redis.io/download">https://redis.io/download</a></p>
</blockquote>
</li>
<li><p>传到 Linux 的 &#x2F;opt 目录下</p>
</li>
<li><p>解压</p>
</li>
<li><p>注意，如果安装的是 6.0 版本及以上的话，需要升级 gcc 到 5.3 及以上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">yum -y install centos-release-scl</span><br><span class="line">yum -y install devtoolset-7*</span><br><span class="line">scl <span class="built_in">enable</span> devtoolset-7 bash</span><br></pre></td></tr></table></figure>

<p>scl 命令启用只是临时的，退出 shell 或重启就会恢复原系统 gcc 版本，如果要长期使用 gcc 7 的话需要执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">&quot;source /opt/rh/devtoolset-7/enable&quot;</span> &gt;&gt;/etc/profile</span><br></pre></td></tr></table></figure>
</li>
<li><p>进入 redis 文件夹中，执行命令<code>make</code>，第一次 make 需要等待较长时间，好了之后再执行<code>make install</code></p>
</li>
<li><p>进入 &#x2F;usr&#x2F;local&#x2F;bin，redis 就装在这里了，可以看到这两个熟悉的东西</p>
<img src="/blog/blog/posts/blog/posts/4e5f8254/image-20201125093215235.png" class>
</li>
<li><p>在这里建立一个文件夹，例如 hlconfig&#x2F;，回到 redis 文件夹中，将里面的 redis.conf 复制到这个文件夹中，以后就修改文件夹中的这个配置文件，原配置文件就不去动了</p>
</li>
<li><p>修改配置文件，将<code>daemonize no</code>改为<code>daemonize yes</code></p>
<ul>
<li><strong>daemonize yes</strong><br>redis 采用的是单进程多线程的模式，当 redis.conf 中选项 daemonize 设置成 yes 时，代表<strong>开启守护进程模式</strong>。在该模式下，redis 会在<strong>后台运行</strong>，并将进程 pid 号写入至 redis.conf 选项 pidfile 设置的文件中，此时 redis 将一直运行，除非手动 kill 该进程。</li>
<li><strong>daemonize no</strong><br>当 daemonize 选项设置成 no 时，当前界面将进入 redis 的命令行界面，exit 强制退出或者关闭连接工具（putty、xshell等）都会导致 redis 进程退出。</li>
</ul>
</li>
<li><p>启动，测试，退出</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">[root@centos01 hlconfig]<span class="comment"># cd /usr/local/bin/</span></span><br><span class="line">[root@centos01 bin]<span class="comment"># redis-server hlconfig/redis.conf </span></span><br><span class="line">[root@centos01 bin]<span class="comment"># redis-cli -p 6379</span></span><br><span class="line">127.0.0.1:6379&gt; ping</span><br><span class="line">PONG</span><br><span class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> name horsen</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get name</span><br><span class="line"><span class="string">&quot;horsen&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; shutdown</span><br><span class="line">not connected&gt; <span class="built_in">exit</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="4-基础知识"><a href="#4-基础知识" class="headerlink" title="4. 基础知识"></a>4. 基础知识</h3><h4 id="4-1-Redis-压力测试工具：Redis-benchmark"><a href="#4-1-Redis-压力测试工具：Redis-benchmark" class="headerlink" title="4.1 Redis 压力测试工具：Redis-benchmark"></a>4.1 Redis 压力测试工具：Redis-benchmark</h4><img src="/blog/blog/posts/blog/posts/4e5f8254/image-20201125101740541.png" class>

<p>可选参数如下表所示：</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>选项</th>
<th>描述</th>
<th>默认值</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>-h</td>
<td>指定服务器主机名</td>
<td>127.0.0.1</td>
</tr>
<tr>
<td>2</td>
<td>-p</td>
<td>指定服务器端口</td>
<td>6379</td>
</tr>
<tr>
<td>3</td>
<td>-s</td>
<td>指定服务器 socket</td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>-c</td>
<td>指定并发连接数</td>
<td>50</td>
</tr>
<tr>
<td>5</td>
<td>-n</td>
<td>指定请求数</td>
<td>10000</td>
</tr>
<tr>
<td>6</td>
<td>-d</td>
<td>以字节的形式指定 SET&#x2F;GET 值的数据大小</td>
<td>2</td>
</tr>
<tr>
<td>7</td>
<td>-k</td>
<td>1&#x3D;keep alive | 0&#x3D;reconnect</td>
<td>1</td>
</tr>
<tr>
<td>8</td>
<td>-r</td>
<td>SET&#x2F;GET&#x2F;INCR 使用随机 key, SADD 使用随机值</td>
<td></td>
</tr>
<tr>
<td>9</td>
<td>-P</td>
<td>通过管道传输 请求</td>
<td>1</td>
</tr>
<tr>
<td>10</td>
<td>-q</td>
<td>强制退出 redis。仅显示 query&#x2F;sec 值</td>
<td></td>
</tr>
<tr>
<td>11</td>
<td>–csv</td>
<td>以 CSV 格式输出</td>
<td></td>
</tr>
<tr>
<td>12</td>
<td>-l</td>
<td>生成循环，永久执行测试</td>
<td></td>
</tr>
<tr>
<td>13</td>
<td>-t</td>
<td>仅运行以逗号分隔的测试命令列表。</td>
<td></td>
</tr>
<tr>
<td>14</td>
<td>-I</td>
<td>Idle 模式。仅打开 N 个 idle 连接并等待。</td>
<td></td>
</tr>
</tbody></table>
<p>测试一下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 100个并发连接，100000个请求，检测host为localhost，端口为6379的redis服务器性能</span></span><br><span class="line">[root@centos01 ~]<span class="comment"># redis-benchmark -h localhost -p 6379 -c 100 -n 100000</span></span><br><span class="line">......</span><br><span class="line">====== SET ====== <span class="comment"># 对集合写入测试</span></span><br><span class="line">  100000 requests completed <span class="keyword">in</span> 2.56 seconds</span><br><span class="line">  100 parallel clients <span class="comment"># 每次请求有100个并发客户端</span></span><br><span class="line">  3 bytes payload <span class="comment"># 每次写入3个字节的数据，有效载荷</span></span><br><span class="line">  keep alive: 1 <span class="comment"># 保持一个连接，一台服务器来处理这些请求</span></span><br><span class="line">  host configuration <span class="string">&quot;save&quot;</span>: 900 1 300 10 60 10000</span><br><span class="line">  host configuration <span class="string">&quot;appendonly&quot;</span>: no</span><br><span class="line">  multi-thread: no</span><br><span class="line"></span><br><span class="line">18.98% &lt;= 1 milliseconds</span><br><span class="line">95.01% &lt;= 2 milliseconds</span><br><span class="line">99.58% &lt;= 3 milliseconds</span><br><span class="line">99.90% &lt;= 4 milliseconds</span><br><span class="line">99.95% &lt;= 5 milliseconds</span><br><span class="line">99.98% &lt;= 6 milliseconds</span><br><span class="line">100.00% &lt;= 6 milliseconds <span class="comment"># 所有请求在 6 毫秒内完成</span></span><br><span class="line">39093.04 requests per second <span class="comment"># 每秒处理 39093.04 次请求</span></span><br><span class="line">......</span><br></pre></td></tr></table></figure>

<h4 id="4-2-常用命令"><a href="#4-2-常用命令" class="headerlink" title="4.2 常用命令"></a>4.2 常用命令</h4><p>默认 16 个数据库，类似数组，下标从零开始，初始默认使用零号库</p>
<p>Select 命令切换数据库，不同的库可以存不同的数据</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; select 7</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[7]&gt;</span><br></pre></td></tr></table></figure>

<p>DBSIZE 查看当前数据库的 key 的数量</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[7]&gt; DBSIZE</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379[7]&gt; select 0</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; DBSIZE</span><br><span class="line">(<span class="built_in">integer</span>) 5</span><br><span class="line">127.0.0.1:6379&gt; keys * <span class="comment"># 查看具体的key</span></span><br><span class="line">1) <span class="string">&quot;counter:__rand_int__&quot;</span></span><br><span class="line">2) <span class="string">&quot;mylist&quot;</span></span><br><span class="line">3) <span class="string">&quot;k1&quot;</span></span><br><span class="line">4) <span class="string">&quot;myset:__rand_int__&quot;</span></span><br><span class="line">5) <span class="string">&quot;key:__rand_int__&quot;</span></span><br></pre></td></tr></table></figure>

<p>FLUSHDB：清空当前库</p>
<p>FLUSHALL：清空所有库</p>
<h4 id="4-3-Redis-是单线程的"><a href="#4-3-Redis-是单线程的" class="headerlink" title="4.3 Redis 是单线程的"></a>4.3 Redis 是单线程的</h4><p>官方表示，因为 Redis 是基于内存的操作，CPU 不是 Redis 的瓶颈，Redis 的瓶颈最有可能是<strong>机器内存的大小或者网络带宽</strong>。既然单线程容易实现，而且 CPU 不会成为瓶颈，那就顺理成章地采用单线程的方案了。</p>
<p>Redis 采用的是基于内存的采用的是单进程单线程模型的 KV 数据库，由 C 语言编写，官方提供的数据是可以达到 100000+ 的 QPS（每秒内查询次数）。这个数据不比采用单进程多线程的同样基于内存的 KV 数据库 Memcached 差。</p>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Horsen Liu
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://horsenliu.gitee.io/blog/posts/4e5f8254.html" title="02_Redis 入门">http://horsenliu.gitee.io/blog/posts/4e5f8254.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/blog/tags/%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/" rel="tag"><i class="fa fa-tag"></i> 环境搭建</a>
              <a href="/blog/tags/Redis/" rel="tag"><i class="fa fa-tag"></i> Redis</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/posts/e84b091a.html" rel="prev" title="01_NoSQL 概述">
      <i class="fa fa-chevron-left"></i> 01_NoSQL 概述
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/posts/8fb13bff.html" rel="next" title="03_五大数据类型">
      03_五大数据类型 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E7%AE%80%E4%BB%8B"><span class="nav-text">1. 简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%8A%9F%E8%83%BD%E5%92%8C%E7%89%B9%E6%80%A7"><span class="nav-text">2. 功能和特性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%AE%89%E8%A3%85-Redis"><span class="nav-text">3. 安装 Redis</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-Windows"><span class="nav-text">3.1 Windows</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-Linux"><span class="nav-text">3.2 Linux</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"><span class="nav-text">4. 基础知识</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-Redis-%E5%8E%8B%E5%8A%9B%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7%EF%BC%9ARedis-benchmark"><span class="nav-text">4.1 Redis 压力测试工具：Redis-benchmark</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="nav-text">4.2 常用命令</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-Redis-%E6%98%AF%E5%8D%95%E7%BA%BF%E7%A8%8B%E7%9A%84"><span class="nav-text">4.3 Redis 是单线程的</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Horsen Liu"
      src="/blog/images/avatar.png">
  <p class="site-author-name" itemprop="name">Horsen Liu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">220</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">123</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/horsenliu" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;horsenliu" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://wpa.qq.com/msgrd?v=3&uin=1468224254&site=qq&menu=yes" title="QQ → https:&#x2F;&#x2F;wpa.qq.com&#x2F;msgrd?v&#x3D;3&amp;uin&#x3D;1468224254&amp;site&#x3D;qq&amp;menu&#x3D;yes" rel="noopener" target="_blank"><i class="fab fa-qq fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://sm.ms/image/jyArGeatQ9Plw4n" title="WeChat → https:&#x2F;&#x2F;sm.ms&#x2F;image&#x2F;jyArGeatQ9Plw4n" rel="noopener" target="_blank"><i class="fab fa-weixin fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:horsenliu@qq.com" title="E-Mail → mailto:horsenliu@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Horsen Liu</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">707k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">10:43</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div><script color="252,100,35" pointColor="252,100,35" opacity="1" zIndex="-1" count="150" src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1/dist/canvas-nest.js"></script>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>

<script src="/blog/js/bookmark.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

<script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/blog/live2dw/assets/assets/hijiki.model.json"},"display":{"superSample":2,"width":180,"height":360,"position":"left","hOffset":40,"vOffset":-60},"mobile":{"show":false,"scale":0.1},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body>
</html>
